package com.fulihui.wgj.admin.api.base;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * 破解微信防盗链方法
 *
 * @auther: Levon
 * @version: V 0.1 2018-01-11 10:14
 */
@Controller
public class ImageProxyController {

    @RequestMapping("/img-proxy")
    public void imageProxy(HttpServletResponse response, HttpServletRequest request) {
        try {
            OutputStream os = response.getOutputStream();
            String picPath = request.getQueryString();
            URLConnection u = new URL(picPath).openConnection();
            InputStream in = u.getInputStream();
            if (null != in) {
                int len;
                byte[] b = new byte[1024];
                while ((len = in.read(b)) != -1) {
                    os.write(b, 0, len);
                }
                os.flush();
                in.close();
            }
            os.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
